package com.gite.nouette.admin.implementation.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.gite.nouette.admin.contract.controller.ICCommand;
import com.gite.nouette.admin.contract.controller.ICModule;
import com.gite.nouette.admin.contract.controller.command.ICCommandModule;
import com.gite.nouette.admin.contract.model.IModule;
import com.gite.nouette.admin.contract.model.command.ICommandModule;
import com.gite.nouette.admin.contract.service.IServiceASync;
import com.gite.nouette.admin.contract.view.IVCommand;
import com.gite.nouette.admin.contract.view.IVModule;

public abstract class AbstractCModule<V extends IVModule<?>, M extends IModule>
		extends AbstractController<V, M> implements ICModule<V, M> {

	@Override
	public IVCommand<?>[] getHeaderVCommands() {
		return getVCommands(getHeaderCCommands());
	}

	private IVCommand<?>[] getVCommands(ICCommand<?, ?>... cCommands) {
		IVCommand<?>[] vCommands = new IVCommand[cCommands.length];
		int i = 0;
		for (ICCommand<?, ?> cCommand : cCommands) {
			vCommands[i] = cCommand.getV();
			i++;
		}

		return vCommands;
	}

	@Override
	public List<ICommandModule> getHeaderCommands() {
		return getM().getHeaderCommands();
	}

	@Override
	public void setHeaderCommands(List<ICommandModule> headerCommands) {
		getM().setHeaderCommands(headerCommands);
	}

	@Override
	public void setHeaderCCommands(ICCommandModule<?, ?>... headerCCommands) {
		List<ICommandModule> cCommands = new ArrayList<ICommandModule>();
		cCommands.addAll(Arrays.asList(headerCCommands));
		setHeaderCommands(cCommands);
	}

	@Override
	public ICCommandModule<?, ?>[] getHeaderCCommands() {
		return getHeaderCommands().toArray(new ICCommandModule[0]);
	}

	@Override
	public IServiceASync getServiceASync() {
		return getM().getServiceASync();
	}

	@Override
	public void setServiceASync(IServiceASync serviceASync) {
		getM().setServiceASync(serviceASync);
	}

}
